<?php

class ChungsController extends AppController {
	public $helpers = array('Html', 'Form', 'Session');
	public $components = array('Session');
	
	public function index() {
		$this->set('posts', $this->Chung->find('all'));
	}
	
	public function view($id) {
		if(!$id) {
			throw new NotFoundException(__('Invalid post'));
		}
		
		$post = $this->Chung->find('first', array('conditions'=>'Chung.id='.$id));
		if(!$post) {
			throw new NotFoundException(__('Invalid post'));
		}
		$this->set('post', $post);
	}
	
	public function add(){
		if($this->request->is('post')) {
			$date = array('TITLE'=>$this->request->data['Post']['TITLE'], 'CREATE_DATE'=>date("Y-m-d H:i:s"), 'UPDATE_DATE'=>date("Y-m-d H:i:s"));
			$this->Chung->Post->set($date);
			$this->Chung->create();
			if($this->Chung->Post->save($this->request->data)) {
				$this->request->data['Chung']['POSTS_ID'] = $this->Chung->Post->id;
				$this->Chung->set($date);
				echo $this->Chung;
				$this->Chung->save($this->request->data);
				$this->Session->setFlash('Your post has been saved.');
				$this->redirect(array('action'=>'../chungs/'));
			} else {
				$this->Session->setFlash('Unable to add your post');
			}
		}
	}
	
	// gio xem thang edit bi loi gi
	public function edit($id){
		if(!$id) {
			throw new NotFoundException(__('Invalid post'));
		}
		
		$post = $this->Chung->find('first', array('conditions'=>'Chung.id='.$id));
		
		if(!$post) {
			throw new NotFoundException(__('Invalid post'));
		}
		
		if($this->request->is('post')) {
			$updata = array('UPDATE_DATE'=>date("Y-m-d H:i:s"));
			
			$this->Chung->Post->set($updata);
			$this->Chung->set($updata);
			if($this->Chung->Post->save($this->request->data) &&
			$this->Chung->save($this->request->data))
			{
				$this->Session->setFlash('Your post has been updated.');
				$this->redirect(array('action'=>'../view/'.$id));
			}
			
			$this->Session->setFlash('Your post has been falure.');
		}
		
		$this->set('post', $post);
	}
}
